package jbox2dprocessing;

import java.util.ArrayList;

import pbox2d.PBox2D;
import jbox2dprocessing.compontent.*;
import processing.core.PApplet;
import processing.core.PFont;



public class Jbox2dProcessing extends PApplet {
	
	public PBox2D box2d;
	ArrayList<AbstractBox2DShape> shape;
	ArrayList<AbstractBox2DShape> boxes;
	
	
	BoundaryBox bottomBoundary;
	HelperShape helperRect;
	Buttons creationButton;
	Buttons moveButton;
	
	MouseInteraction mouseDrag;
	
	float creationStartMouseX;
	float creationStartMouseY;
	float creationEndMouseX;
	float creationEndMouseY;
	
	int mouseStat = 0;
	
	//	-1 -- Mouse hover the buttons;  0 -- Creation;  1 -- Move;
	int appStat = 0;
	PFont appStatFont;
	String appStatText = "Creation Mode, Drag mouse to creat shapes, Press E to change mode";
	
	public void setup() {
		size(800,600);
		smooth();
		AbstractBox2DShape.parent = this;
		BaseBox.parent = this;
		HelperShape.parent = this;
		Buttons.parent = this;
		MouseInteraction.parent = this;
		
		box2d = new PBox2D(this);
		box2d.createWorld();
		
		box2d.setGravity(0, -100);
		
		boxes = new ArrayList<AbstractBox2DShape>();
		bottomBoundary = new BoundaryBox(400, 575, 800, 50, box2d);
		
		helperRect = new HelperShape();
		creationButton = new Buttons(50, 20, color(255,0,0), 0);
		moveButton = new Buttons(170, 20, color(0,0,255), 1);
		
		PhysCircle p = new PhysCircle(width/2, height/2, 50, box2d);
		boxes.add(p);
		
		 
	}

	public void draw() {
		background(255);
		appStatFont = createFont("FFScala", 16);
		textFont(appStatFont);
		fill(0,102,153);
		text(appStatText, 20, 50);
		
		box2d.step();
		
		bottomBoundary.display();
		
//		println(this.hashCode());
		
//		creationButton.display();
//		moveButton.display();
//		
//		creationButton.buttonEventListener(Buttons.MOUSEOVER, mouseX, mouseY);
//		moveButton.buttonEventListener(Buttons.MOUSEOVER, mouseX, mouseY);
//		
//		if(creationButton.buttonEventListener(Buttons.MOUSEOVER, mouseX, mouseY) || moveButton.buttonEventListener(Buttons.MOUSEOVER, mouseX, mouseY)){
//			appStat = -1;
//		}
//		else if(!creationButton.buttonEventListener(Buttons.MOUSEOVER, mouseX, mouseY) && !moveButton.buttonEventListener(Buttons.MOUSEOVER, mouseX, mouseY)){
//			appStat = 0;
//		}
		
		helperRect.updateHelper();
		
//		if(mousePressed){
//			PhysBox p = new PhysBox(mouseX, mouseY, 15, 15, box2d);
//			boxes.add(p);
//		}
				
		//	Display the PhysBoxes
		for (int i = 0; i < boxes.size(); i++) {
			AbstractBox2DShape p = (AbstractBox2DShape)boxes.get(i);
			p.display();
		}
				
		//	Remove the PhysBoxes when they are out of screen
		for(int i = boxes.size() - 1; i >=0; i--){
			AbstractBox2DShape p = (AbstractBox2DShape)boxes.get(i);
			if(p.outOfScreen()){
				boxes.remove(i);
			}
		}
	}
	
	public void mousePressed(){
		mouseStat = 1;
//		mouseDrag = new MouseInteraction(box2d);
//		if(mouseDrag.getBodyAtMouse() == null){
//			startCreation();
//			helperRect.helperStartX = mouseX;
//			helperRect.helperStartY = mouseY;
//		}
//		else if(mouseDrag.getBodyAtMouse() != null){
//			
//			mouseDrag.bind(mouseX,mouseY, mouseDrag.getBodyAtMouse());
//			mouseDrag.update(mouseX,mouseY);
//		}
		

		
		
		
		if(appStat == 0){
			startCreation();
			helperRect.helperStartX = mouseX;
			helperRect.helperStartY = mouseY;
		}
		else if(appStat == -1){
			
		}
		else if(appStat == 1){
			mouseDrag = new MouseInteraction(box2d);
			if(mouseDrag.getBodyAtMouse() != null){
				mouseDrag.bind(mouseX,mouseY, mouseDrag.getBodyAtMouse());
			}
			mouseDrag.update(mouseX,mouseY);
		}
	}
	
	public void mouseDragged(){
		mouseStat = 2;
		
//		if(mouseDrag.getBodyAtMouse() == null){
//			startCreation();
//			helperRect.helperEndX = mouseX;
//			helperRect.helperEndY = mouseY;
//		}
//		else if(mouseDrag.getBodyAtMouse() != null){
//			mouseDrag.update(mouseX,mouseY);
//		}

		if(appStat == 0){
			helperRect.helperEndX = mouseX;
			helperRect.helperEndY = mouseY;
		}
		else if(appStat == -1){
			
		}
		else if(appStat == 1){
			mouseDrag.update(mouseX,mouseY);
		}
	}
	
	public void mouseMoved(){
		helperRect.helperStartX = mouseX;
		helperRect.helperStartY = mouseY;
		helperRect.helperEndX = mouseX;
		helperRect.helperEndY = mouseY;
	}
	
	public void mouseReleased(){
//		mouseStat = 0;
//		if(mouseDrag.getBodyAtMouse() == null){
//			endCreation();
//		}
//		else if(mouseDrag.getBodyAtMouse() != null){
//			mouseDrag.destory();
//		}

		if(appStat == 0)
			endCreation();
		else if(appStat == -1){
			
		}
		else if(appStat == 1){
			mouseDrag.destory();
		}
	}
	
	public void mouseClicked(){
		if(appStat == 0){
			
		}
		else if(appStat == -1){
			
		}
		else if(appStat == 1){
			
		}
	}
	
	public void keyReleased(){
		if(key == 'c'){
			appStat = 0;
			appStatText = "Creation Mode, Drag mouse to creat shapes, Press E to change mode";
		}
		else if(key == 'e'){
			appStat = 1;
			appStatText = "Editing Mode, Drag on the shapes to move, Press C to change mode";
		}
	}
	
	public void startCreation(){
		creationStartMouseX = mouseX;
		creationStartMouseY = mouseY;
	}
	
	public void endCreation(){
		helperRect.hideHelper();
		
		creationEndMouseX = mouseX;
		creationEndMouseY = mouseY;
		
		float boxPosX = (creationStartMouseX + creationEndMouseX)/2;
		float boxPosY = (creationStartMouseY + creationEndMouseY)/2;
		float boxWidth = Math.abs(creationEndMouseX - creationStartMouseX);
		float boxHeight = Math.abs(creationEndMouseY - creationStartMouseY);
		
		PhysBox p = new PhysBox(boxPosX, boxPosY, boxWidth, boxHeight, box2d);
		boxes.add(p);
		
	}
	
	
	
	public static void main(String _args[]) {
		PApplet.main(new String[] { jbox2dprocessing.Jbox2dProcessing.class.getName() });
	}
}
